package org.jeecg.modules.power.timing;

import lombok.RequiredArgsConstructor;
import org.jeecg.modules.app.v1.util.Const;
import org.jeecg.modules.power.device.entity.YlEmptyShopRecords;
import org.jeecg.modules.power.device.service.IYlDeviceRecordsService;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 业务：门店解绑设备后7天内没有绑定设备自动进入公海
 *
 * @author scw
 * @version v1.0
 * @date 2024-08-07
 */
@Component
@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class WeaningShopTask {

    private final IYlDeviceRecordsService deviceRecordsService;
    private final RedisTemplate<String, Object> redisTemplate;


    @Scheduled(cron = "0 0 0 * * ?")//每天凌晨 0点 执行任务
    public void WeaningShopTask(){
        List<YlEmptyShopRecords>list = deviceRecordsService.emptyShopList();
        LocalDateTime now = LocalDateTime.now();
        for(YlEmptyShopRecords records:list) {
            LocalDateTime dateTime = LocalDateTime.parse(records.getUnbindTime());
            long seconds = ChronoUnit.SECONDS.between(now, dateTime);
            String key = Const.getEmptyShopKey(records.getShopId());
            /**
             * 创建延时任务，根据seconds后执行进入公海业务
             */
            redisTemplate.opsForValue().set(key, records.getShopId(), seconds, TimeUnit.SECONDS);
        }
    }


}
